package com.neu.his.controller.lastVerssion;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.core.Result;
import com.neu.his.entity.*;
import com.neu.his.entity.VO.lastVerssion.DiagRecordsVO;
import com.neu.his.entity.VO.lastVerssion.OldDRVO;
import com.neu.his.entity.VO.lastVerssion.RegistRecordsVO;
import com.neu.his.entity.request.lastVerssion.DiagParams;
import com.neu.his.entity.request.lastVerssion.DiagRecordsParams;
import com.neu.his.entity.request.lastVerssion.RegistRecordsParams;
import com.neu.his.entity.request.lastVerssion.SelectRegistParams;
import com.neu.his.mapper.*;
import com.neu.his.utils.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/19 14:38
 */
@RestController
@RequestMapping("/recordHome")
public class RecordHomeController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    RegistMapper registMapper;

    @Autowired
    DiagRecordMapper diagRecordMapper;

    @Autowired
    UserDepartmentMapper userDepartmentMapper;

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    DiseaseMapper diseaseMapper;

    @PostMapping("/registRecords")
    public Result<PageList<RegistRecordsVO>> getRegistList(@RequestBody RegistRecordsParams registRecordsParams) {
        List<RegistRecordsVO> result = new ArrayList<RegistRecordsVO>();
        Long departmentid = userDepartmentMapper.selectOne(new QueryWrapper<UserDepartment>()
                .eq("user_id", registRecordsParams.getUserId()).ne("remove_mark", 1L)).getDepartmentId();
        List<Regist> list = registMapper.selectList(new QueryWrapper<Regist>().ne("remove_mark", 1L)
                .eq("department_id", departmentid).ne("have_deliver", true));
        for (Regist regist : list) {
            Patient patient = patientMapper.selectOne(new QueryWrapper<Patient>()
                    .ne("remove_mark", 1L).eq("id", regist.getPatientId()));
            RegistRecordsVO registRecordsVO = new RegistRecordsVO();
            registRecordsVO.setPtId(regist.getPatientId());
            registRecordsVO.setPtName(patient.getName());
            registRecordsVO.setRegistId(regist.getId());
            result.add(registRecordsVO);
        }
        return Result.success(new PageList(result, registRecordsParams.getCurPage(), registRecordsParams.getPageSize()));
    }

    @PostMapping("/diagRecords")
    public Result<PageList<DiagRecordsVO>> getDiagList(@RequestBody DiagRecordsParams diagRecordsParams) {
        List<DiagRecordsVO> result = new ArrayList<>();
        List<DiagRecords> list = diagRecordMapper.selectList(new QueryWrapper<DiagRecords>()
                .eq("doctor_id", diagRecordsParams.getUserId()).ne("remove_mark", 1L));
        for (DiagRecords diagRecords : list) {
            Patient patient = patientMapper.selectOne(new QueryWrapper<Patient>()
                    .ne("remove_mark", 1L).eq("id", diagRecords.getPatientId()));
            DiagRecordsVO diagRecordsVO = new DiagRecordsVO();
            diagRecordsVO.setDiagId(diagRecords.getId());
            diagRecordsVO.setPtId(patient.getId());
            diagRecordsVO.setPtName(patient.getName());
            result.add(diagRecordsVO);
        }
        return Result.success(new PageList<>(result, diagRecordsParams.getCurPage(), diagRecordsParams.getPageSize()));
    }

    @PostMapping("/selectRegist")
    public Result selectRegist(@RequestBody SelectRegistParams selectRegistParams) {
        Regist regist = registMapper.selectOne(new QueryWrapper<Regist>().ne("remove_mark", 1L).eq("id", selectRegistParams.getRegistId()));
        regist.setHaveDeliver(true);
        registMapper.updateById(regist);
        DiagRecords diagRecords = new DiagRecords();
        diagRecords.setCreateDate();
        diagRecords.setDoctorId(selectRegistParams.getUserId());
        diagRecords.setPatientId(regist.getPatientId());
        diagRecords.setTotalCost(new BigDecimal(0));
        diagRecords.setRealCost(new BigDecimal(0));
        diagRecordMapper.insert(diagRecords);
        return Result.success();
    }

    @PostMapping("/selectDiag")
    public Result selectDiag(@RequestBody DiagParams diagParams) {
        Long id = diagParams.getDiagId();
        DiagRecords diagRecords = diagRecordMapper.selectOne(new QueryWrapper<DiagRecords>()
                .ne("remove_mark", 1L).eq("id", id));
        return Result.success(diagRecords);
    }

    @PostMapping("/updateDiag")
    public Result updateDiag(@RequestBody DiagRecords diagRecords) {
        UpdateWrapper<DiagRecords> updateWrapper = new UpdateWrapper();
        updateWrapper.ne("remove_mark", 1L);
        updateWrapper.eq("id", diagRecords.getId());
        updateWrapper.set(StringUtils.isNotEmpty(diagRecords.getDiseaseHistory()), "disease_history", diagRecords.getDiseaseHistory());
        updateWrapper.set(StringUtils.isNotEmpty(diagRecords.getInvoiceNumber()), "invoice_number", diagRecords.getInvoiceNumber());
        updateWrapper.set(StringUtils.isNotEmpty(diagRecords.getMedicalAdvice()), "medical_advice", diagRecords.getMedicalAdvice());
        updateWrapper.set(diagRecords.getStartHospitalization() != null, "start_hospitalization", diagRecords.getStartHospitalization());
        updateWrapper.set(diagRecords.getEndHospitalization() != null, "end_hospitalization", diagRecords.getEndHospitalization());
        diagRecordMapper.update(null, updateWrapper);
        return Result.success();
    }

    @GetMapping("/getDisease")
    public Result getDisease() {
        List<Disease> result = diseaseMapper.selectList(new QueryWrapper<>());
        return Result.success(result);
    }

    @PostMapping("/allRecords")
    public Result<PageList<OldDRVO>> allRecords(@RequestBody DiagRecordsParams diagRecordsParams){
        List<OldDRVO> result = new ArrayList<>();
        List<DiagRecords> list = diagRecordMapper.selectList(new QueryWrapper<DiagRecords>()
                .eq("doctor_id", diagRecordsParams.getUserId()));
        for (DiagRecords diagRecords : list) {
            Patient patient = patientMapper.selectOne(new QueryWrapper<Patient>()
                    .ne("remove_mark", 1L).eq("id", diagRecords.getPatientId()));
            OldDRVO oldDRVO = new OldDRVO();
            oldDRVO.setDiagId(diagRecords.getId());
            oldDRVO.setPtId(patient.getId());
            oldDRVO.setPtName(patient.getName());
            oldDRVO.setMedicalAdvice(diagRecords.getMedicalAdvice());
            result.add(oldDRVO);
        }
        return Result.success(new PageList<>(result, diagRecordsParams.getCurPage(), diagRecordsParams.getPageSize()));
    }

    @PostMapping("/selectOldDiag")
    public Result selectOldDiag(@RequestBody DiagParams diagParams) {
        Long id = diagParams.getDiagId();
        DiagRecords diagRecords = diagRecordMapper.selectOne(new QueryWrapper<DiagRecords>().eq("id", id));
        return Result.success(diagRecords);
    }
}
